package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.common.cache.RedisCache;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;
    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;


	@Override
	@RedisCache(prefix = RedisConstant.BASE_CATEGORY_LIST)
	public List<JSONObject> getBaseCategoryList() {
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		if(CollectionUtils.isEmpty(baseCategoryViews)){
			return List.of();
		}
		List<JSONObject> jsonObjects = new ArrayList<>();
		//根据一级id分类
		Map<Long, List<BaseCategoryView>> category2sGroupBy1IdMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

		if(!CollectionUtils.isEmpty(category2sGroupBy1IdMap)){
			for (Map.Entry<Long, List<BaseCategoryView>> baseCategoryViewEntry : category2sGroupBy1IdMap.entrySet()) {
				JSONObject jsonObject1 = new JSONObject();
				Long category1Id = baseCategoryViewEntry.getKey();
				List<BaseCategoryView> baseCategoryView2s = baseCategoryViewEntry.getValue();
				String category1Name = baseCategoryView2s.get(0).getCategory1Name();
				jsonObject1.put("categoryId",category1Id);
				jsonObject1.put("categoryName",category1Name);
				List<JSONObject> category2Child = new ArrayList<>();
				//根据二级id分类
				Map<Long, List<BaseCategoryView>> category3sGroupBy2IdMap = baseCategoryView2s.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
				if(!CollectionUtils.isEmpty(category3sGroupBy2IdMap)){
					for (Map.Entry<Long, List<BaseCategoryView>> baseCategoryView3sEntry : category3sGroupBy2IdMap.entrySet()) {
						JSONObject jsonObject2 = new JSONObject();
						Long category2Id = baseCategoryView3sEntry.getKey();
						List<BaseCategoryView> baseCategoryView3s = baseCategoryView3sEntry.getValue();
						jsonObject2.put("categoryId",category2Id);
						jsonObject2.put("categoryName",baseCategoryView3s.get(0).getCategory2Name());
						List<JSONObject> category3Child = baseCategoryView3s.stream().map(item -> {
							JSONObject jsonObject = new JSONObject();
							jsonObject.put("categoryId", item.getCategory3Id());
							jsonObject.put("categoryName", item.getCategory3Name());
							return jsonObject;
						}).collect(Collectors.toList());
						jsonObject2.put("categoryChild",category3Child);
						category2Child.add(jsonObject2);
					}
					jsonObject1.put("categoryChild",category2Child);
				}
				jsonObjects.add(jsonObject1);
			}
		}
		return jsonObjects;

	}


	/**
	 *
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.findAttributeByCategory1Id(category1Id);
	}

	@Override
	public BaseCategoryView getCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		if(CollectionUtil.isNotEmpty(baseCategory2s)){
			Set<Long> cagegory2Ids = baseCategory2s.stream().map(BaseCategory2::getId).collect(Collectors.toSet());
			List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(new LambdaQueryWrapper<BaseCategory3>()
					.in(BaseCategory3::getCategory2Id, cagegory2Ids)
					.eq(BaseCategory3::getIsTop, 1)
					.eq(BaseCategory3::getIsDeleted, 0)
					.orderByAsc(BaseCategory3::getOrderNum)
					.last("limit 7"));
			return baseCategory3s;
		}
		return List.of();
	}

	@Override
	public JSONObject getBaseCategoryListByCategory1Id(Long category1Id) {
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>()
				.eq(BaseCategoryView::getCategory1Id,category1Id));
		JSONObject jsonObject1 = new JSONObject();
		if(CollectionUtil.isNotEmpty(baseCategoryViews)){
			Long category1Id1 = baseCategoryViews.get(0).getCategory1Id();
			String category1Name = baseCategoryViews.get(0).getCategory1Name();
			jsonObject1.put("categoryId",category1Id1);
			jsonObject1.put("categoryName",category1Name);
			List<JSONObject> category2Child = new ArrayList<>();
			//根据二级id分类
			Map<Long, List<BaseCategoryView>> category3sGroupBy2IdMap = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			if(!CollectionUtils.isEmpty(category3sGroupBy2IdMap)){
				for (Map.Entry<Long, List<BaseCategoryView>> baseCategoryView3sEntry : category3sGroupBy2IdMap.entrySet()) {
					JSONObject jsonObject2 = new JSONObject();
					Long category2Id = baseCategoryView3sEntry.getKey();
					List<BaseCategoryView> baseCategoryView3s = baseCategoryView3sEntry.getValue();
					jsonObject2.put("categoryId",category2Id);
					jsonObject2.put("categoryName",baseCategoryView3s.get(0).getCategory2Name());
					List<JSONObject> category3Child = baseCategoryView3s.stream().map(item -> {
						JSONObject jsonObject = new JSONObject();
						jsonObject.put("categoryId", item.getCategory3Id());
						jsonObject.put("categoryName", item.getCategory3Name());
						return jsonObject;
					}).collect(Collectors.toList());
					jsonObject2.put("categoryChild",category3Child);
					category2Child.add(jsonObject2);
				}
				jsonObject1.put("categoryChild",category2Child);
			}
		}
		return jsonObject1;
	}

	@Override
	public List<BaseCategory1> findAllCategory1() {
		List<BaseCategory1> list = baseCategory1Mapper.selectList(new LambdaQueryWrapper<BaseCategory1>().eq(BaseCategory1::getIsDeleted, 0));
		return list;
	}
}
